package com.shopping_back.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping_back.mapper.OrderMainMapper;
import com.shopping_back.pojo.entity.OrderMain;
import com.shopping_back.pojo.entity.RefundRecord;
import com.shopping_back.mapper.RefundRecordMapper;
import com.shopping_back.service.IRefundRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 退款记录表 服务实现类
 * </p>
 *
 * @author Jack
 * @since 2025-07-12
 */
@Service
@RequiredArgsConstructor
public class RefundRecordServiceImpl extends ServiceImpl<RefundRecordMapper, RefundRecord> implements IRefundRecordService {

    private final RefundRecordMapper refundRecordMapper;

    private final OrderMainMapper orderMainMapper;
    @Override
    public Page<RefundRecord> getRefunds(Long merchantId, Integer page, Integer size) {
        Page<RefundRecord> pageInfo = new Page<>(page, size);
        List<RefundRecord> refunds = refundRecordMapper.selectRefundsByMerchantId(merchantId, size, (page - 1) * size);
        pageInfo.setRecords(refunds);
        pageInfo.setTotal(refundRecordMapper.countRefundsByMerchantId(merchantId));
        return pageInfo;
    }

    @Transactional
    public void approveRefund(Long paymentId, String refundSn, Long merchantId) {
        RefundRecord refund = refundRecordMapper.selectOne(
                new LambdaQueryWrapper<RefundRecord>()
                        .eq(RefundRecord::getPaymentId, paymentId)
                        .eq(RefundRecord::getRefundSn, refundSn));
        if (refund == null) {
            throw new IllegalArgumentException("退款记录不存在");
        }
        OrderMain order = orderMainMapper.selectById(paymentId);
        if (order == null) {
            throw new IllegalArgumentException("订单不存在");
        }
        if (!order.getMerchantId().equals(merchantId)) {
            throw new IllegalArgumentException("无权限操作此退款记录");
        }
        if (refund.getStatus() != 0) {
            throw new IllegalArgumentException("退款记录状态不允许操作");
        }
        if (order.getStatus() != 5) {
            throw new IllegalArgumentException("订单状态必须为待退款");
        }
        if (refund.getRefundAmount().compareTo(order.getPayAmount()) > 0) {
            throw new IllegalArgumentException("退款金额不能超过实际支付金额 ¥" + order.getPayAmount());
        }
        // 更新退款记录状态为已退款
        refund.setStatus(1);
        refundRecordMapper.updateById(refund);
        // 更新订单状态为已退款
        order.setStatus(6);
        orderMainMapper.updateById(order);
    }

    @Transactional
    public void rejectRefund(Long paymentId, String refundSn, Long merchantId) {
        RefundRecord refund = refundRecordMapper.selectOne(
                new LambdaQueryWrapper<RefundRecord>()
                        .eq(RefundRecord::getPaymentId, paymentId)
                        .eq(RefundRecord::getRefundSn, refundSn));
        if (refund == null) {
            throw new IllegalArgumentException("退款记录不存在");
        }
        OrderMain order = orderMainMapper.selectById(paymentId);
        if (order == null) {
            throw new IllegalArgumentException("订单不存在");
        }
        if (!order.getMerchantId().equals(merchantId)) {
            throw new IllegalArgumentException("无权限操作此退款记录");
        }
        if (refund.getStatus() != 0) {
            throw new IllegalArgumentException("退款记录状态不允许操作");
        }
        if (order.getStatus() != 5) {
            throw new IllegalArgumentException("订单状态必须为待退款");
        }
        // 更新退款记录状态为拒绝
        refund.setStatus(2);
        refundRecordMapper.updateById(refund);
        // 更新订单状态为退款失败
        order.setStatus(7);
        orderMainMapper.updateById(order);
    }


}
